﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
#if !WINDOWS_PHONE


#endif

namespace DARE
{
    [AttrTypeDependentSerializable(typeof(DARE.Save.DataStructures.CBoxModelData))]
    public class CBoxModel : CBasicModel
    {

    #region ctor

        public CBoxModel() : 
            base()
        {
            Initialize();
        }

        public CBoxModel(string name) : 
            base(name)
        {
            Initialize();
        }

        public CBoxModel(float x, float y, float z) : 
            base(x, y, z)
        {
            Initialize();
        }

        public CBoxModel(string name, float x, float y, float z) : 
            base(name, x, y, z)
        {
            Initialize();
        }

        private void Initialize()
        {
            //m_cullMode = CullMode.None;
            CullMode = CullMode.None;
            SetupVertices();
            CreateVertexBuffer();
        }

        public void CreateBox()
        {

        }

        public override AModel Clone()
        {
            CBoxModel clone = new CBoxModel(m_name, m_size.X, m_size.Y, m_size.Z);

            base.CopyPropertiesTo(clone);
            return clone;
        }

    #endregion

    #region setup

        protected void SetupVertices()
        {
#if WINDOWS_PHONE
            m_vertices = new VertexPositionNormalTexture[36];
#else
            m_vertices = new VertexPositionNormalTextureTangentBinormal[36];
#endif
            m_size /= 2.0f;
            
            // points definition
            Vector3 topLeftFront = new Vector3(-m_size.X, m_size.Y, m_size.Z);
            Vector3 bottomLeftFront = new Vector3(-m_size.X, -m_size.Y, m_size.Z);
            Vector3 topRightFront = new Vector3(m_size.X, m_size.Y, m_size.Z);
            Vector3 bottomRightFront = new Vector3(m_size.X, -m_size.Y, m_size.Z);
            Vector3 topLeftBack = new Vector3(-m_size.X, m_size.Y, -m_size.Z);
            Vector3 topRightBack = new Vector3(m_size.X, m_size.Y, -m_size.Z);
            Vector3 bottomLeftBack = new Vector3(-m_size.X, -m_size.Y, -m_size.Z);
            Vector3 bottomRightBack = new Vector3(m_size.X, -m_size.Y, -m_size.Z);
#if WINDOWS_PHONE
            // front face
            m_vertices[0] = new VertexPositionNormalTexture(topRightFront, Vector3.UnitZ, Vector2.UnitX);
            m_vertices[1] = new VertexPositionNormalTexture(bottomRightFront, Vector3.UnitZ, Vector2.One);
            m_vertices[2] = new VertexPositionNormalTexture(bottomLeftFront, Vector3.UnitZ, Vector2.UnitY);
            m_vertices[3] = new VertexPositionNormalTexture(topRightFront, Vector3.UnitZ, Vector2.UnitX);
            m_vertices[4] = new VertexPositionNormalTexture(bottomLeftFront, Vector3.UnitZ, Vector2.UnitY);
            m_vertices[5] = new VertexPositionNormalTexture(topLeftFront, Vector3.UnitZ, Vector2.Zero);

            // back face
            m_vertices[6] = new VertexPositionNormalTexture(bottomRightBack, -Vector3.UnitZ, Vector2.One);
            m_vertices[7] = new VertexPositionNormalTexture(topRightBack, -Vector3.UnitZ, Vector2.UnitX);
            m_vertices[8] = new VertexPositionNormalTexture(bottomLeftBack, -Vector3.UnitZ, Vector2.UnitY);
            m_vertices[9] = new VertexPositionNormalTexture(bottomLeftBack, -Vector3.UnitZ, Vector2.UnitY);
            m_vertices[10] = new VertexPositionNormalTexture(topRightBack, -Vector3.UnitZ, Vector2.UnitX);
            m_vertices[11] = new VertexPositionNormalTexture(topLeftBack, -Vector3.UnitZ, Vector2.Zero);

            // top face
            m_vertices[12] = new VertexPositionNormalTexture(topRightBack, Vector3.UnitY, Vector2.UnitX);
            m_vertices[13] = new VertexPositionNormalTexture(topRightFront, Vector3.UnitY, Vector2.One);
            m_vertices[14] = new VertexPositionNormalTexture(topLeftFront, Vector3.UnitY, Vector2.UnitY);
            m_vertices[15] = new VertexPositionNormalTexture(topLeftBack, Vector3.UnitY, Vector2.Zero);
            m_vertices[16] = new VertexPositionNormalTexture(topRightBack, Vector3.UnitY, Vector2.UnitX);
            m_vertices[17] = new VertexPositionNormalTexture(topLeftFront, Vector3.UnitY, Vector2.UnitY);

            // bottom face
            m_vertices[18] = new VertexPositionNormalTexture(bottomRightFront, -Vector3.UnitY, Vector2.UnitX);
            m_vertices[19] = new VertexPositionNormalTexture(bottomRightBack, -Vector3.UnitY, Vector2.One);
            m_vertices[20] = new VertexPositionNormalTexture(bottomLeftFront, -Vector3.UnitY, Vector2.Zero);
            m_vertices[21] = new VertexPositionNormalTexture(bottomRightBack, -Vector3.UnitY, Vector2.One);
            m_vertices[22] = new VertexPositionNormalTexture(bottomLeftBack, -Vector3.UnitY, Vector2.UnitY);
            m_vertices[23] = new VertexPositionNormalTexture(bottomLeftFront, -Vector3.UnitY, Vector2.Zero);

            // left face
            m_vertices[24] = new VertexPositionNormalTexture(topLeftFront, -Vector3.UnitX, Vector2.UnitX);
            m_vertices[25] = new VertexPositionNormalTexture(bottomLeftBack, -Vector3.UnitX, Vector2.UnitY);
            m_vertices[26] = new VertexPositionNormalTexture(topLeftBack, -Vector3.UnitX, Vector2.Zero);
            m_vertices[27] = new VertexPositionNormalTexture(bottomLeftFront, -Vector3.UnitX, Vector2.One);
            m_vertices[28] = new VertexPositionNormalTexture(bottomLeftBack, -Vector3.UnitX, Vector2.UnitY);
            m_vertices[29] = new VertexPositionNormalTexture(topLeftFront, -Vector3.UnitX, Vector2.UnitX);

            // right face
            m_vertices[30] = new VertexPositionNormalTexture(bottomRightBack, Vector3.UnitX, Vector2.One);
            m_vertices[31] = new VertexPositionNormalTexture(topRightFront, Vector3.UnitX, Vector2.Zero);
            m_vertices[32] = new VertexPositionNormalTexture(topRightBack, Vector3.UnitX, Vector2.UnitX);
            m_vertices[33] = new VertexPositionNormalTexture(bottomRightBack, Vector3.UnitX, Vector2.One);
            m_vertices[34] = new VertexPositionNormalTexture(bottomRightFront, Vector3.UnitX, Vector2.UnitY);
            m_vertices[35] = new VertexPositionNormalTexture(topRightFront, Vector3.UnitX, Vector2.Zero);
#else
            // front face
            m_vertices[0] = new VertexPositionNormalTextureTangentBinormal(topRightFront, Vector3.UnitZ, Vector2.UnitX);
            m_vertices[1] = new VertexPositionNormalTextureTangentBinormal(bottomRightFront, Vector3.UnitZ, Vector2.One);
            m_vertices[2] = new VertexPositionNormalTextureTangentBinormal(bottomLeftFront, Vector3.UnitZ, Vector2.UnitY);
            m_vertices[3] = new VertexPositionNormalTextureTangentBinormal(topRightFront, Vector3.UnitZ, Vector2.UnitX);
            m_vertices[4] = new VertexPositionNormalTextureTangentBinormal(bottomLeftFront, Vector3.UnitZ, Vector2.UnitY);
            m_vertices[5] = new VertexPositionNormalTextureTangentBinormal(topLeftFront, Vector3.UnitZ, Vector2.Zero);

            // back face
            m_vertices[6] = new VertexPositionNormalTextureTangentBinormal(bottomRightBack, -Vector3.UnitZ, Vector2.One);
            m_vertices[7] = new VertexPositionNormalTextureTangentBinormal(topRightBack, -Vector3.UnitZ, Vector2.UnitX);
            m_vertices[8] = new VertexPositionNormalTextureTangentBinormal(bottomLeftBack, -Vector3.UnitZ, Vector2.UnitY);
            m_vertices[9] = new VertexPositionNormalTextureTangentBinormal(bottomLeftBack, -Vector3.UnitZ, Vector2.UnitY);
            m_vertices[10] = new VertexPositionNormalTextureTangentBinormal(topRightBack, -Vector3.UnitZ, Vector2.UnitX);
            m_vertices[11] = new VertexPositionNormalTextureTangentBinormal(topLeftBack, -Vector3.UnitZ, Vector2.Zero);

            // top face
            m_vertices[12] = new VertexPositionNormalTextureTangentBinormal(topRightBack, Vector3.UnitY, Vector2.UnitX);
            m_vertices[13] = new VertexPositionNormalTextureTangentBinormal(topRightFront, Vector3.UnitY, Vector2.One);
            m_vertices[14] = new VertexPositionNormalTextureTangentBinormal(topLeftFront, Vector3.UnitY, Vector2.UnitY);
            m_vertices[15] = new VertexPositionNormalTextureTangentBinormal(topLeftBack, Vector3.UnitY, Vector2.Zero);
            m_vertices[16] = new VertexPositionNormalTextureTangentBinormal(topRightBack, Vector3.UnitY, Vector2.UnitX);
            m_vertices[17] = new VertexPositionNormalTextureTangentBinormal(topLeftFront, Vector3.UnitY, Vector2.UnitY);

            // bottom face
            m_vertices[18] = new VertexPositionNormalTextureTangentBinormal(bottomRightFront, -Vector3.UnitY, Vector2.UnitX);
            m_vertices[19] = new VertexPositionNormalTextureTangentBinormal(bottomRightBack, -Vector3.UnitY, Vector2.One);
            m_vertices[20] = new VertexPositionNormalTextureTangentBinormal(bottomLeftFront, -Vector3.UnitY, Vector2.Zero);
            m_vertices[21] = new VertexPositionNormalTextureTangentBinormal(bottomRightBack, -Vector3.UnitY, Vector2.One);
            m_vertices[22] = new VertexPositionNormalTextureTangentBinormal(bottomLeftBack, -Vector3.UnitY, Vector2.UnitY);
            m_vertices[23] = new VertexPositionNormalTextureTangentBinormal(bottomLeftFront, -Vector3.UnitY, Vector2.Zero);

            // left face
            m_vertices[24] = new VertexPositionNormalTextureTangentBinormal(topLeftFront, -Vector3.UnitX, Vector2.UnitX);
            m_vertices[25] = new VertexPositionNormalTextureTangentBinormal(bottomLeftBack, -Vector3.UnitX, Vector2.UnitY);
            m_vertices[26] = new VertexPositionNormalTextureTangentBinormal(topLeftBack, -Vector3.UnitX, Vector2.Zero);
            m_vertices[27] = new VertexPositionNormalTextureTangentBinormal(bottomLeftFront, -Vector3.UnitX, Vector2.One);
            m_vertices[28] = new VertexPositionNormalTextureTangentBinormal(bottomLeftBack, -Vector3.UnitX, Vector2.UnitY);
            m_vertices[29] = new VertexPositionNormalTextureTangentBinormal(topLeftFront, -Vector3.UnitX, Vector2.UnitX);

            // right face
            m_vertices[30] = new VertexPositionNormalTextureTangentBinormal(bottomRightBack, Vector3.UnitX, Vector2.One);
            m_vertices[31] = new VertexPositionNormalTextureTangentBinormal(topRightFront, Vector3.UnitX, Vector2.Zero);
            m_vertices[32] = new VertexPositionNormalTextureTangentBinormal(topRightBack, Vector3.UnitX, Vector2.UnitX);
            m_vertices[33] = new VertexPositionNormalTextureTangentBinormal(bottomRightBack, Vector3.UnitX, Vector2.One);
            m_vertices[34] = new VertexPositionNormalTextureTangentBinormal(bottomRightFront, Vector3.UnitX, Vector2.UnitY);
            m_vertices[35] = new VertexPositionNormalTextureTangentBinormal(topRightFront, Vector3.UnitX, Vector2.Zero);
#endif
        }

    #endregion

    }
}
